//
//  LustDetailsViewController.m
//  safeLady
//
//  Created by Justsoso on 2017/7/10.
//  Copyright © 2017年 xmy. All rights reserved.
//

#import "LustDetailsViewController.h"
#import "LustDataCollectionViewCell.h"
#import "LoveTimesCollectionViewCell.h"
#import "LustDetailsViewModel.h"
#import "PopExplainView.h"
#import "PopTool.h"
#import "CalendarViewController.h"

@interface LustDetailsViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) UIView *backgroundView;
@property (nonatomic, strong) UIView *topView;

@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *contentLabel;

@property (nonatomic, strong) UICollectionView *lustDataCollectionView;
@property (nonatomic, strong) UICollectionView *loveTimesCollectionView;

@property (nonatomic, strong) NSIndexPath *selectIndexPath;

@property (nonatomic, strong) LustDetailsViewModel *viewModel;

@property (nonatomic, strong) NSArray *loveTimes;
@property (nonatomic, strong) NSDictionary *lustDataDic;

@property (nonatomic, strong) PopExplainView *popExplainView;



@end

@implementation LustDetailsViewController

@synthesize viewModel = _viewModel;

- (NSDictionary *)lustDataDic {
    if (!_lustDataDic) {
        _lustDataDic = [NSDictionary dictionary];
    }
    return _lustDataDic;
}

- (NSArray *)loveTimes {
    if (!_loveTimes) {
        _loveTimes = [NSArray array];
    }
    return _loveTimes;
}

- (PopExplainView *)popExplainView {
    if (!_popExplainView) {
        _popExplainView = [[PopExplainView alloc] init];
        _popExplainView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        _popExplainView.dismissBlock = ^(PopExplainView *popExplainView) {
            [[PopTool sharedInstance] closeAnimated:YES];
        };
    }
    return _popExplainView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"我的情欲";
    

    self.view.backgroundColor = [UIColor colorWithHex:0xF7F7F7];
    [[[self.navigationController.navigationBar subviews] objectAtIndex:0] setAlpha:0];
    
    self.backgroundView = [[UIView alloc] initWithFrame:CGRectMake(10, 74, self.view.width-20, self.view.height-94)];
    self.backgroundView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.backgroundView];
    
    
    self.backgroundView.cornerRadius = 20;
    self.backgroundView.shadowColor = [UIColor lightGrayColor];
    self.backgroundView.shadowOffset = CGSizeMake(0, 2);
    self.backgroundView.shadowRadius = 4;
    self.backgroundView.shadowOpacity = 0.9;
    
    [self initView];
    
//    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:self.view.bounds];
//    toolbar.barStyle = UIBarStyleDefault;
//    [self.view addSubview:toolbar];
}

- (void)initView {
    
    [self addtopView];
    
    [self addLoveTimesView];
    
    [self addLustDataView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)addtopView {
    
    self.topView = [[UIView alloc] init];
    self.topView.backgroundColor = [UIColor clearColor];
    [self.backgroundView addSubview:self.topView];
    [self.topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backgroundView.mas_top);
        make.centerX.equalTo(self.backgroundView.mas_centerX);
        make.height.equalTo(@(H(120)));
        make.left.equalTo(self.backgroundView.mas_left);
    }];
    
    UIImageView *image = [[UIImageView alloc] init];
    image.backgroundColor = [UIColor colorWithHex:0xDBDBE3];
    image.layer.cornerRadius = H(30);
    [self.topView addSubview:image];
    [image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(H(40)));
        make.centerY.equalTo(self.topView.mas_centerY);
        make.width.height.equalTo(@(H(60)));
    }];
    
    self.titleLabel = [[UILabel alloc] init];
    self.titleLabel.text = @"猛爆型";
    self.titleLabel.textColor = [UIColor colorWithHex:0xB0B1C9];
    self.titleLabel.font = [UIFont boldSystemFontOfSize:25.f];
    [self.topView addSubview:self.titleLabel];
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(image.mas_centerY).offset(-5);
        make.height.equalTo(@15);
        make.left.equalTo(image.mas_right).offset(10);
    }];
    
    self.contentLabel = [[UILabel alloc] init];
    self.contentLabel.text = @"静如处子动若惊鸿，你控制的很好哦";
    self.contentLabel.numberOfLines = 0;
    self.contentLabel.textColor = [UIColor colorWithHex:0x6A696A];
    self.contentLabel.font = [UIFont systemFontOfSize:12.f];
    [self.topView addSubview:self.contentLabel];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.titleLabel.mas_left);
        make.top.equalTo(image.mas_centerY).offset(5);
        make.width.equalTo(@160);
    }];
}


/**
 添加爱爱次数列表视图
 */
- (void)addLoveTimesView {
    CGFloat width = self.backgroundView.frame.size.width / 8;
    CGFloat height = self.backgroundView.frame.size.height - H(120) - (self.backgroundView.frame.size.width * 0.6);
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake(width, height);
    flow.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flow.minimumLineSpacing = 0;
    flow.minimumInteritemSpacing = 0;
    
    self.loveTimesCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flow];
    self.loveTimesCollectionView.delegate = self;
    self.loveTimesCollectionView.dataSource = self;
    self.loveTimesCollectionView.showsHorizontalScrollIndicator = NO;
    self.loveTimesCollectionView.backgroundColor = [UIColor whiteColor];
    [self.backgroundView addSubview:self.loveTimesCollectionView];
    [self.loveTimesCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.topView.mas_bottom);
        make.centerX.equalTo(self.backgroundView.mas_centerX);
        make.left.equalTo(self.backgroundView.mas_left);
        make.height.equalTo(@(height));
    }];
    
    
    
    UILabel *topLine = [[UILabel alloc] init];
    topLine.backgroundColor = [UIColor colorWithHex:0xf7f7f7];
    [self.backgroundView addSubview:topLine];
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.backgroundView.mas_centerX);
        make.left.equalTo(self.backgroundView.mas_left);
        make.height.equalTo(@(H(1)));
        make.top.equalTo(self.loveTimesCollectionView.mas_top);
    }];
    
    
    
    UILabel *bottomLine = [[UILabel alloc] init];
    bottomLine.backgroundColor = [UIColor colorWithHex:0xf7f7f7];
    [self.backgroundView addSubview:bottomLine];
    [bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.backgroundView.mas_centerX);
        make.left.equalTo(self.backgroundView.mas_left);
        make.height.equalTo(@(H(1)));
        make.bottom.equalTo(self.loveTimesCollectionView.mas_bottom);
    }];
    /* 注册自定义单元格 */
    [self.loveTimesCollectionView registerClass:[LoveTimesCollectionViewCell class] forCellWithReuseIdentifier:@"LoveTimesCell"];
    
    
}


/**
 详细数据
 */
- (void)addLustDataView {
    
    /* 设置单元格布局 */
    CGFloat width = self.backgroundView.frame.size.width / 3;
    CGFloat height = self.backgroundView.frame.size.width / 5;
    UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc] init];
    flow.itemSize = CGSizeMake(width, height);
    flow.minimumLineSpacing = 0;
    flow.minimumInteritemSpacing = 0;
    
    
    /* 创建集合视图 */
    self.lustDataCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) collectionViewLayout:flow];
    self.lustDataCollectionView.delegate = self;
    self.lustDataCollectionView.dataSource = self;
    self.lustDataCollectionView.backgroundColor = [UIColor whiteColor];
    self.lustDataCollectionView.cornerRadius = 20;
    [self.backgroundView addSubview:self.lustDataCollectionView];
    
    [self.lustDataCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.backgroundView.mas_bottom);
        make.centerX.equalTo(self.backgroundView.mas_centerX);
        make.width.equalTo(@(width * 3));
        make.height.equalTo(@(height * 3));
    }];
    
    self.lustDataCollectionView.scrollEnabled = NO;
    
    /* 注册自定义单元格 */
    [self.lustDataCollectionView registerClass:[LustDataCollectionViewCell class] forCellWithReuseIdentifier:@"LustDataCell"];
    
    
    
}


/**
 设置集合视图单元格数量
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    if (collectionView == self.lustDataCollectionView) {
        return 9;
    }else {
        return self.loveTimes.count;
    }
    
}


/**
 设置集合视图
 */
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (collectionView == self.lustDataCollectionView) {
        LustDataCollectionViewCell *lustDatacell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LustDataCell" forIndexPath:indexPath];
        
        
        
        switch (indexPath.row) {
            case 0:
                lustDatacell.titleLabel.text = @"前戏期";
                
                lustDatacell.contentLabel.attributedText = [self setCollectionCellContent:self.lustDataDic[@"foreplay_period"] == nil ? @"1000 min" :[NSString stringWithFormat:@"%@ min",self.lustDataDic[@"foreplay_period"]]];
                [lustDatacell.rightLabel setHidden:NO];
                [lustDatacell.bottomLabel setHidden:NO];

                break;
            case 1:
                lustDatacell.titleLabel.text = @"快感期";
                
                lustDatacell.contentLabel.attributedText = [self setCollectionCellContent:self.lustDataDic[@"pleasure_stage"] == nil ? @"1000 min" :[NSString stringWithFormat:@"%@ min",self.lustDataDic[@"pleasure_stage"]]];
                [lustDatacell.bottomLabel setHidden:YES];
                [lustDatacell.rightLabel setHidden:NO];
                
                
                break;
            case 2:
                lustDatacell.titleLabel.text = @"高潮期";
                
                lustDatacell.contentLabel.attributedText = [self setCollectionCellContent:self.lustDataDic[@"high_tide"] == nil ? @"1000 min" :[NSString stringWithFormat:@"%@ min",self.lustDataDic[@"high_tide"]]];
                [lustDatacell.rightLabel setHidden:YES];
                [lustDatacell.bottomLabel setHidden:NO];
                break;
            case 3:
                lustDatacell.titleLabel.text = @"消耗卡路里";
                
                lustDatacell.contentLabel.attributedText = [self setCollectionCellContent:self.lustDataDic[@"burn_calories"] == nil ? @"1000 min" :[NSString stringWithFormat:@"%@ 千卡",self.lustDataDic[@"burn_calories"]]];
                [lustDatacell.rightLabel setHidden:YES];
                [lustDatacell.bottomLabel setHidden:NO];
                
                break;
            case 4:
                lustDatacell.titleLabel.text = nil;
                lustDatacell.contentLabel.text = nil;
                
                [lustDatacell.rightLabel setHidden:YES];
                [lustDatacell.bottomLabel setHidden:YES];
                
                break;
            case 5:
                lustDatacell.titleLabel.text = @"震动次数";
                
                lustDatacell.contentLabel.attributedText = [self setCollectionCellContent:self.lustDataDic[@"vibration_times"] == nil ? @"1000 min" :[NSString stringWithFormat:@"%@ min",self.lustDataDic[@"vibration_times"]]];
                
                [lustDatacell.bottomLabel setHidden:NO];
                [lustDatacell.rightLabel setHidden:YES];
                break;
                
            case 6:
                lustDatacell.titleLabel.text = @"私处收缩";
                
                lustDatacell.contentLabel.attributedText = [self setCollectionCellContent:self.lustDataDic[@"pressure"] == nil ? @"1000 min" :[NSString stringWithFormat:@"%@ mmHG",self.lustDataDic[@"pressure"]]];
                
                [lustDatacell.bottomLabel setHidden:YES];
                [lustDatacell.rightLabel setHidden:NO];
                
                break;
            case 7:
                lustDatacell.titleLabel.text = @"私处湿度";
                
                lustDatacell.contentLabel.attributedText = [self setCollectionCellContent:self.lustDataDic[@"humidity"] == nil ? @"1000 min" :[NSString stringWithFormat:@"%@ %%",self.lustDataDic[@"humidity"]]];
                
                [lustDatacell.bottomLabel setHidden:YES];
                [lustDatacell.rightLabel setHidden:NO];
                
                
                break;
            case 8:
                lustDatacell.titleLabel.text = @"总时长";
                
                lustDatacell.contentLabel.attributedText = [self setCollectionCellContent:self.lustDataDic[@"total_duration"] == nil ? @"1000 min" :[NSString stringWithFormat:@"%@ min",self.lustDataDic[@"total_duration"]]];
                
                [lustDatacell.bottomLabel setHidden:YES];
                [lustDatacell.rightLabel setHidden:YES];
                break;
            default:
                break;
                
        }
        return lustDatacell;
    }
    
    if (collectionView == self.loveTimesCollectionView) {
        LoveTimesCollectionViewCell *loveTimesCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LoveTimesCell" forIndexPath:indexPath];
        
        
        NSDictionary *dic = self.loveTimes[indexPath.row];
        
        CGFloat radHeight = [dic[@"score_erotic"] intValue] / 200.0 * 100;
        CGFloat height = H(radHeight) == 0 || H(radHeight) > W(8) ? H(radHeight) : W(8);
        
        NSString *str = dic[@"add_time"];
        int month = [[str substringWithRange:NSMakeRange(5, 2)] intValue];
        int day = [[str substringWithRange:NSMakeRange(8, 2)] intValue];
        int hour = [[str substringWithRange:NSMakeRange(11, 2)] intValue];
        int min = [[str substringWithRange:NSMakeRange(14, 2)] intValue];
        
        loveTimesCell.dateLabel.text = [NSString stringWithFormat:@"%d/%d",day,month];
        loveTimesCell.timeLabel.text = [NSString stringWithFormat:@"%d:%d",hour,min];
        loveTimesCell.dataViewHeight = height;
        loveTimesCell.dataViewWidth = W(8);
        loveTimesCell.dataView.cornerRadius = W(8) / 2;
        
        if (self.selectIndexPath.row == indexPath.row) {
            loveTimesCell.dataViewWidth = W(20);
            loveTimesCell.dataView.cornerRadius = W(20) / 2;
            loveTimesCell.dataView.backgroundColor = [UIColor colorWithHex:0x9395B1];
            [self.lustDataCollectionView reloadData];
        }
        if (indexPath.row == self.loveTimes.count - 2) {
            self.lustDataDic = self.loveTimes[indexPath.row];
        }
        
        
        
        return loveTimesCell;
    }
    
    
    return nil;
    
    
}




/**
 富文本：修改空格后面的文字大小

 @param content 传入内容
 @return 返回富文本
 */
- (NSAttributedString *)setCollectionCellContent:(NSString *)content {
    
    NSString *str = @" ";
    NSRange range = [content rangeOfString:str];
    NSInteger location = range.location;
    NSMutableAttributedString *str2 = [[NSMutableAttributedString alloc] initWithString:content];
    [str2 addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:10.f] range:NSMakeRange(location, content.length - location)];
    return str2;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.loveTimesCollectionView) {
        LoveTimesCollectionViewCell *cell = (LoveTimesCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        
        self.selectIndexPath = indexPath;
        
        self.lustDataDic = self.loveTimes[indexPath.row];
        
        cell.dataViewWidth = W(15);
        cell.dataView.cornerRadius = W(15) / 2;
        cell.dataView.backgroundColor = [UIColor colorWithHex:0x9395B1];
        
        if ([self.lustDataDic[@"score_erotic"] intValue] >= 110) {
            self.titleLabel.text = @"猛爆型";
            self.contentLabel.text = @"静如处子动若惊鸿，你控制的很好哦";
        }else if ([self.lustDataDic[@"score_erotic"] intValue] >= 80) {
            self.titleLabel.text = @"激动型";
            self.contentLabel.text = arc4random()%2 ? @"传说中的性瘾都快望尘莫及啦":@"就是这样强烈到不能自控";
        }else if ([self.lustDataDic[@"score_erotic"] intValue] >= 50) {
            self.titleLabel.text = @"苦恼型";
            self.contentLabel.text = arc4random()%2 ? @"性爱要追求质量噢":@"性爱要追求质量噢";
        }else if ([self.lustDataDic[@"score_erotic"] intValue] < 50) {
            self.titleLabel.text = @"冷漠型";
            self.contentLabel.text = arc4random()%2 ? @"你这么冷漠你知道么":@"传说中的性冷淡也跟不上你啦";
        }
        
        [self.lustDataCollectionView reloadData];
    }else if (collectionView == self.lustDataCollectionView) {
        if (indexPath.row != 4) {
            LustDataCollectionViewCell *cell = (LustDataCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
            
            self.popExplainView.title = cell.titleLabel.text;
            [[PopTool sharedInstance] popView:self.popExplainView animated:YES];
            
        }
        
    }
}

- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView == self.loveTimesCollectionView) {
        LoveTimesCollectionViewCell *cell = (LoveTimesCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
        
        self.selectIndexPath = nil;
        
        cell.dataViewWidth = W(8);
        cell.dataView.cornerRadius = W(8) / 2;
        cell.dataView.backgroundColor = [UIColor gradientFromColor:[UIColor colorWithHex:0x9396B1] toColor:[UIColor colorWithHex:0xDBDBE4] withHeight:cell.dataView.height + 1];
        
    }
}

- (LustDetailsViewModel *)viewModel {
    if (_viewModel == nil) {
        _viewModel = [[LustDetailsViewModel alloc] initWithViewController:self];
    }
    return _viewModel;
}

- (void)bindViewModel {
    [super bindViewModel];
    @weakify(self);
    [RACObserve(self.viewModel, userLoveTimes) subscribeNext:^(id x) {
        @strongify(self);
        
        self.loveTimes = self.viewModel.userLoveTimes;
        [self.loveTimesCollectionView reloadData];
    }];
}

//- (void)collectionView:(UICollectionView *)collectionView didUnhighlightItemAtIndexPath:(NSIndexPath *)indexPath {
//    if (collectionView == self.loveTimesCollectionView) {
//        LoveTimesCollectionViewCell *cell = (LoveTimesCollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
//        
////        cell.dataView.centerX = cell.centerX;
//        cell.dataView.width = W(8);
//        cell.dataView.cornerRadius = W(8) / 2;
//        
//    }
//}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
